//
//  TJSingleChatController.m
//  XWC
//
//  Created by Hello on 2022/10/24.
//

#import "HJSingleChatController.h"
#import "HJIM.h"
#import "HJChatMessageLayoutHeader.h"
#import <Photos/Photos.h>
#import "HJSingleChatSettingController.h"
#import "HJUserInfoController.h"

@interface HJSingleChatController ()

@end

@implementation HJSingleChatController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.rightBarButton = [HJBarButton barButtonWithImage:[UIImage imageNamed:@"setting_icon"] target:self action:@selector(didClickSettingButton)];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRefreshUserInfoNotification:) name:refreshUserInfoNotification object:nil];
    [self loadUserInfo];
}

- (void)didClickSettingButton {
    HJUserInfoController *infoController = [[HJUserInfoController alloc] init];
    infoController.userId = TJIM_isSelf(self.sessionModel.senderId) ? self.sessionModel.receiveId : self.sessionModel.uid;;
    [self.navigationController pushViewController:infoController];
}

- (void)handleRefreshUserInfoNotification:(NSNotification *)not {
    NSString *sessionId = not.object;
    if ([sessionId isEqualToString:self.sessionModel.sessionId]) {
        [self loadUserInfo];
    }
}


- (void)loadUserInfo {
   
    [[HJIM shareIM].userManager loadFriendInfoWithUserId:self.sessionModel.sessionUserd complete:^(HJChatFriendModel * _Nullable model) {
        
        if (!model) return;
        
        BOOL isUpdate = !([model.avatar isEqualToString:self.sessionModel.sessionAvatar] && [model.nickname isEqualToString:self.sessionModel.sessionName]);
        if (isUpdate) {
            [HJIM.shareIM.conversationManager getConversationBySessionId:self.sessionModel.sessionId completion:^(HJChatSessionModel * _Nonnull sessionModel) {
                sessionModel.sessionAvatar = model.avatar;
                sessionModel.sessionName = model.showName;
                [sessionModel bg_saveOrUpdate];
                [[NSNotificationCenter defaultCenter] postNotificationName:updateConversationInfoNotification object:sessionModel];
            }];
        }
    }];
    
}

- (void)didClickMoreButton {
    
}

- (void)dealloc {
    TJLog(@"%s", __func__);
}


@end
